package com.example.fragment;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.shop.DBHelper;
import com.example.shop.Order;
import com.example.shop.R;

import java.util.ArrayList;

public class FragmentOrder extends Fragment {

    ListView dingdan_lv;
    ArrayList<Order> order;
    View view;
    ArrayList<String> titleList, datelist, pricelist;
    TextView titleItemFragment1;
    TextView pubmanItemFragment1;
    TextView pubtimeItemFragment1;
    //当前用户名
    private String user;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_order, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        dingdan_lv = view.findViewById(R.id.dingdan_lv);
        //获取当前用户名
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("name", 0);  /*获取名称为name的sharedPreferences*/
        user = sharedPreferences.getString("name", "");
        //标题
        TextView tv_main_title = view.findViewById(R.id.tv_main_title);
        tv_main_title.setText("我的订单");
        //返回键
        ImageView iv_back = view.findViewById(R.id.iv_back);
        //标题布局
        RelativeLayout rl_title_bar = view.findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.rgb(255, 165, 0));
        //返回键的点击事件
        iv_back.setVisibility(View.GONE);


        DBHelper dbHelper = new DBHelper(getContext());
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        Cursor cursor = database.query("dingdan", null, null, null, null, null, "date");
        cursor.moveToFirst();
        order = new ArrayList<>();
        titleList = new ArrayList<>();
        datelist = new ArrayList<>();
        pricelist = new ArrayList<>();
        int t = 0;
        while (!cursor.isAfterLast()) {
            Order shu = new Order();
            //显示当前用户订单
            if (cursor.getString(cursor.getColumnIndex("user")).equals(user)) {
                shu.setId(cursor.getInt(cursor.getColumnIndex("_id")));
                shu.setPrice(cursor.getString(cursor.getColumnIndex("price")));
                shu.setName(cursor.getString(cursor.getColumnIndex("title")));
                shu.setDate(cursor.getString(cursor.getColumnIndex("date")));
                order.add(shu);
                titleList.add(order.get(t).getName());
                datelist.add(order.get(t).getDate());
                pricelist.add(order.get(t).getPrice());
                t++;
            }
            cursor.moveToNext();
        }
        dingdan_lv.setAdapter(new MyAdapter());
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return titleList.size();
        }

        @Override
        public Object getItem(int position) {
            return titleList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView != null) {
                return convertView;
            }
            convertView = View.inflate(getContext(), R.layout.dingdan_item, null);

            titleItemFragment1 = (TextView) convertView.findViewById(R.id.title_item_fragment1);
            pubmanItemFragment1 = (TextView) convertView.findViewById(R.id.pubman_item_fragment1);
            pubtimeItemFragment1 = (TextView) convertView.findViewById(R.id.pubtime_item_fragment1);

            titleItemFragment1.setText(titleList.get(position));
            pubmanItemFragment1.setText(pricelist.get(position));
            pubtimeItemFragment1.setText(datelist.get(position));

            return convertView;
        }
    }
}
